package com.aiyige.page.user;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.detail.DetailPage;
import com.aiyige.page.user.adapter.ProductAdapter;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.widget.CommonDataView;
import com.aiyige.base.BaseFragment;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class ProductPage extends BaseFragment implements CommonDataView.RetrofitStub {

    @BindView(R.id.cdv)
    CommonDataView cdv;
    Unbinder unbinder;
    ProductAdapter productAdapter;
    List<String> subjectList = Arrays.asList(Moment.SUBJECT_VIDEO_COURSE, Moment.SUBJECT_MAJOR_COURSE/*, Moment.SUBJECT_TRAINING_COURSE, Moment.SUBJECT_TRAINING_CARD*/);
    String userId;

    public ProductPage() {

    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public static ProductPage newInstance(String userId) {
        ProductPage fragment = new ProductPage();
        fragment.setUserId(userId);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.page_product, container, false);
        unbinder = ButterKnife.bind(this, view);
        productAdapter = new ProductAdapter();
        cdv.config(productAdapter, this);
        productAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Moment moment = productAdapter.getItem(position);
                switch (moment.getSubjectIndex()) {
                    case Moment.SUBJECT_VIDEO_COURSE_INDEX:
                        Router.start(moment.getRouter());
                        break;
                    default:
                        Router.start(moment.getRouter());
                        break;
                }
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initData() {
        cdv.doRefreshRequest();
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().productService(userId, subjectList, 15, currentPage, 1);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<Moment> momentList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                momentList.addAll(JSON.parseArray(page.getContent(), Moment.class));
            }
            for (Moment moment : momentList) {
                resultList.add(new Moment(moment));
            }
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
